﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace ChatServeur
{
    class Serveur
    {
        string name;
        IPAddress ip;
        int port;
        Socket socket;
        Stream stream;
        StreamReader reader;
        StreamWriter writer;
        int file;
        LinkedList<Client> users;

        public Serveur()
        {
            file = 10;

            Console.WriteLine("Nom du serveur :");
            name = Console.ReadLine();

            Console.WriteLine("\n" + "Port :");
            port = Convert.ToInt16(Console.ReadLine());

            ip = IPAddress.Any;

            Launching(ip);
        }

        public void Launching(IPAddress IP)
        {
            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(new IPEndPoint(IP, port));
                socket.Connect(IP, port);
                socket.Listen(file);
                stream = new NetworkStream(socket);
                reader = new StreamReader(stream);
                writer = new StreamWriter(stream);
            }
            catch (SocketException exc)
            {
                Console.WriteLine("\n" + "Erreur : " + exc);
                Console.ReadLine();
            }
        }

        public void RecieveClient()
        {
            while (true)
            {
                Client client;
                Socket socketClient;

                socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socketClient.Accept();

                client = new Client(socketClient);
                Welcome();
            }
        }

        public void Welcome()
        {
            writer.WriteLine("Bienvenu!");
        }


    }
}
